import { dynamic, MicroApp, history } from 'umi';
import LoadingComponent from '@/components/PageLoading';
import { currentAllApp } from '@/services/app'
import store from 'store'

let extraRoutes = [];

export const qiankun = currentAllApp()
  .then(({ code, data }) => {
    extraRoutes = data;
    return Promise.resolve({
      // 注册子应用信息
      apps: data,
      // 完整生命周期钩子请看 https://qiankun.umijs.org/zh/api/#registermicroapps-apps-lifecycles
      lifeCycles: {
        afterMount: (props) => {
          console.log(props);
        },
      },
      // 支持更多的其他配置，详细看这里 https://qiankun.umijs.org/zh/api/#start-opts
    });
  });

export function patchRoutes({ routes }) {
  // debugger
  extraRoutes.forEach((element) => {
    routes[1].routes[0].routes.push({
      name: element.name,
      icon: 'smile',
      path: element.activeRule,
      component: dynamic({
        loader: () =>
          import(<MicroApp name={element.name} />),
        loading: LoadingComponent,
      }),
    });
  });
  console.log(routes)
}

export async function render(oldRender) {
  oldRender();
}